package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.OfferLineOrderBeans;
import com.itboye.textileexpes.beans.OfferLineOrderBeans.IdCCCC.Items;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpes.presenter.ShopCartPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.ClickListener;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexpres.widget.MyListView;
import com.itboye.textileexpres.widget.XListView;
import com.itboye.textileexptes.config.GlobalConfig;

public class ActivityOffLine extends BaseActivity implements OnClickListener,
		Observer {
	ImageView back;
	TextView title;
	ImageView img_other;
	OfferLineOrderBeans beans;
	ClickListener listener;
	// OfferLineOrderBeans lineOrderBeans = new OfferLineOrderBeans();
	private String page_size = "10";
	private String page_index = "1";
	LinearLayout off_lin;
	XListView listview;
	ShopCartPresenter cartPresenter;
	listAdapter adapter;
	long createtime;
	MyOrderPresenter myOrderPresenter;
	List<Items> items;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.acitivity_off;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText("线下订单");
		img_other.setVisibility(View.GONE);
		if (IsMobileNumPhone.isUid() == null
				|| "".equals(IsMobileNumPhone.isUid())) {

		}
		beans = new OfferLineOrderBeans();
		cartPresenter = new ShopCartPresenter(this);
		cartPresenter.onShopCharOrder(IsMobileNumPhone.isUid(), page_size,
				page_index);
		myOrderPresenter = new MyOrderPresenter(this);
		items = new ArrayList<OfferLineOrderBeans.IdCCCC.Items>();

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {

			if (entity.getEventType() == cartPresenter.ShopcarOrderSuccessEvent) {
				// toast("bbb"+entity.getData());
				beans = (OfferLineOrderBeans) entity.getData();
				adapter = new listAdapter(ActivityOffLine.this, beans);
				listview.setAdapter(adapter);

			} else if (entity.getEventType() == myOrderPresenter.Fenye_success) {
				toast(entity.getData() + "");
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	class listAdapter extends BaseAdapter {
		Context context;
		OfferLineOrderBeans lineOrderBeans;

		public listAdapter(Context context) {
			// TODO Auto-generated constructor stub
			this.context = context;
		}

		public listAdapter(Context context, OfferLineOrderBeans lineOrderBeans) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.lineOrderBeans = lineOrderBeans;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lineOrderBeans.getList().size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			viewHolder holder = null;
			if (convertView == null) {
				holder = new viewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_offline_expand, null);
				holder.create_time = (TextView) convertView
						.findViewById(R.id.create_time);
				holder.delete_tv = (TextView) convertView
						.findViewById(R.id.delete_tv);
				convertView.setTag(holder);
			} else {
				holder = (viewHolder) convertView.getTag();
			}
			MyAdapter myadapter = new MyAdapter(context, lineOrderBeans
					.getList().get(position).getProcessed_order_items());
			createtime = lineOrderBeans.getList().get(position)
					.getCreate_time();
			holder.expandlist = (MyListView) convertView
					.findViewById(R.id.expandlist);
			holder.create_time.setText(TimeFormat.DateFormat(createtime + ""));
			holder.expandlist.setAdapter(myadapter);
			// holder.delete_tv.setTag(position);
			// holder.delete_tv.setOnClickListener(this);
			return convertView;
		}

	}

	class viewHolder {
		LinearLayout cotainer;
		TextView create_time, delete_tv;
		MyListView expandlist;
	}

	class MyAdapter extends BaseAdapter {
		Context context;
		List<OfferLineOrderBeans.IdCCCC.Items> items;

		public MyAdapter(Context context,
				List<OfferLineOrderBeans.IdCCCC.Items> items) {

			// TODO Auto-generated constructor stub
			this.context = context;
			this.items = items;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub

			return items.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			viewMyListViewHolder myListViewHolder = null;
			if (convertView == null) {
				myListViewHolder = new viewMyListViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.offline_childview, null);
				convertView.setTag(myListViewHolder);
				LinearLayout childview_lin = (LinearLayout) convertView
						.findViewById(R.id.childview_lin);
				myListViewHolder.area = (TextView) convertView
						.findViewById(R.id.area);
				myListViewHolder.name = (TextView) convertView
						.findViewById(R.id.name);
				childview_lin.removeAllViews();
				for (int i = 0; i < items.get(position).getItems().size(); i++) {
					View view = LayoutInflater.from(context).inflate(
							R.layout.offline_childview_item, null);
					ImageView img = (ImageView) view.findViewById(R.id.img);
					TextView goods_name = (TextView) view
							.findViewById(R.id.goods_name);
					TextView goods_context = (TextView) view
							.findViewById(R.id.goods_context);
					LinearLayout lineyout = (LinearLayout) convertView
							.findViewById(R.id.lineyout);
					XImageLoader.load(
							GlobalConfig.IMAGE_DOUBLE
									+ items.get(position).getItems().get(i)
											.getId(), img);
					goods_name.setText(items.get(position).getItems().get(i)
							.getName());
					goods_context.setText(items.get(position).getItems().get(i)
							.getSecondary());
					childview_lin.addView(view);
				}
			} else {
				myListViewHolder = (viewMyListViewHolder) convertView.getTag();
			}
			myListViewHolder.area
					.setText(items.get(position).getPlace_origin());
			myListViewHolder.name.setText(items.get(position).getContact_name()
					+ "");
			return convertView;
		}
	}

	class viewMyListViewHolder {
		TextView name, area;
		LinearLayout lineyout;

	}

	public void setListViewHeightBasedOnChildren(ListView listView) {
		// 获取ListView对应的Adapter
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
			// listAdapter.getCount()返回数据项的数目
			View listItem = listAdapter.getView(i, null, listView);
			// 计算子项View 的宽高
			listItem.measure(0, 0);
			// 统计所有子项的总高度
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

}
